package common.util;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import common.util.exception.InvalidDateException;

// Referenced classes of package com.kingdee.util:
//            InvalidDateException

public class DateTimeUtils
{

    public DateTimeUtils()
    {
    }

    public static String format(Date d, String fmt)
    {
        return format(d, fmt, null);
    }

    public static String format(Date d, String fmt, TimeZone timezone)
    {
        return format(d, fmt, timezone, null);
    }

    public static String format(Date d, String fmt, TimeZone timezone, Locale locale)
    {
        if(fmt == null)
            fmt = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat df;
        if(locale == null)
            df = new SimpleDateFormat(fmt);
        else
            df = new SimpleDateFormat(fmt, locale);
        if(timezone == null)
            df.setTimeZone(TimeZone.getDefault());
        else
            df.setTimeZone(timezone);
        return df.format(d);
    }

    public static String format(Date d)
    {
        return format(d, null, null);
    }

    public static String format(Date d, TimeZone timezone)
    {
        return format(d, null, timezone);
    }

    public static String formatDate(Date d, TimeZone timezone)
    {
        return format(d, "yyyy-MM-dd", timezone);
    }

    public static String formatDate(Date d)
    {
        return formatDate(d, null);
    }

    public static String formatTime(Date d, TimeZone timezone)
    {
        return format(d, "HH:mm:ss", timezone);
    }

    public static String formatTime(Date d)
    {
        return formatTime(d, null);
    }

    public static String defaultDateString(Date d)
    {
        return defaultDateString(d, null);
    }

    public static String defaultDateString(Date d, TimeZone timezone)
    {
        if(timezone == null)
            timezone = TimeZone.getDefault();
        long l = timezone.getRawOffset();
        StringBuffer temp = new StringBuffer(format(d, timezone));
        if(l > 0L)
        {
            temp.append(" +");
        } else
        {
            temp.append(" -");
            l = -l;
        }
        temp.append(format(new Date(l), "HH:mm", gmt));
        return temp.toString();
    }

    public static Date parseDate(String s)
        throws ParseException
    {
        try
        {
            return parseDate(s, "yyyy-MM-dd HH:mm:ss");
        }
        catch(ParseException e) { }
        try
        {
            return parseDate(s, "yyyy-MM-dd");
        }
        catch(ParseException e) { }
        try
        {
            return parseDate(s, "HH:mm:ss");
        }
        catch(ParseException e)
        {
            throw new ParseException("can not udstand your format", -1);
        }
    }

    public static Date parseDate(String s, String fmt)
        throws ParseException
    {
        return parseDate(s, fmt, null);
    }

    public static Date parseDate(String s, String fmt, TimeZone timezone)
        throws ParseException
    {
        return parseDate(s, fmt, timezone, null);
    }

    public static Date parseDate(String s, String fmt, TimeZone timezone, Locale locale)
        throws ParseException
    {
        SimpleDateFormat df;
        if(locale == null)
            df = new SimpleDateFormat(fmt);
        else
            df = new SimpleDateFormat(fmt, locale);
        if(timezone == null)
            timezone = TimeZone.getDefault();
        df.setTimeZone(timezone);
        df.setLenient(false);
        return df.parse(s);
    }

    public static Date parseDefaultDate(String s)
        throws ParseException
    {
        int i = s.lastIndexOf(' ');
        char c = s.charAt(i + 1);
        long l = parseDate(s.substring(i + 2), "HH:mm", gmt).getTime();
        if(c == '-')
            l = -l;
        return parseDate(s.substring(0, i), "yyyy-MM-dd HH:mm:ss", new SimpleTimeZone((int)l, "XXX"));
    }

    public static String emailDate(Date d)
    {
        return emailDate(d, null);
    }

    public static String emailDate(Date d, TimeZone timezone)
    {
        if(timezone == null)
            timezone = TimeZone.getDefault();
        long l = timezone.getRawOffset();
        StringBuffer temp = new StringBuffer(format(d, "E, d MMM yyyy HH:mm:ss", timezone, Locale.UK));
        if(l > 0L)
        {
            temp.append(" +");
        } else
        {
            temp.append(" -");
            l = -l;
        }
        temp.append(format(new Date(l), "HHmm", gmt));
        return temp.toString();
    }

    public static Date parseEmailDate(String s)
        throws ParseException
    {
        try
        {
            int i = s.lastIndexOf(' ');
            char c = s.charAt(i + 1);
            long l = parseDate(s.substring(i + 2), "HHmm", gmt).getTime();
            if(c == '-')
                l = -l;
            return parseDate(s.substring(0, i), "E, d MMM yyyy HH:mm:ss", new SimpleTimeZone((int)l, "XXX"), Locale.UK);
        }
        catch(Exception e)
        {
            return parseDate(s, "E, d MMM yyyy HH:mm:ss z", gmt, Locale.UK);
        }
    }

    public static boolean dayBefore(Date dt, Date dt1)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        int year = c.get(1);
        int date = c.get(6);
        c.setTime(dt1);
        int year1 = c.get(1);
        int date1 = c.get(6);
        return year < year1 || year == year1 && date < date1;
    }

    public static boolean dayAfter(Date dt, Date dt1)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        int year = c.get(1);
        int date = c.get(6);
        c.setTime(dt1);
        int year1 = c.get(1);
        int date1 = c.get(6);
        return year > year1 || year == year1 && date > date1;
    }

    public static boolean dayEquals(Date dt, Date dt1)
    {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        int year = c.get(1);
        int date = c.get(6);
        c.setTime(dt1);
        int year1 = c.get(1);
        int date1 = c.get(6);
        return year == year1 && date == date1;
    }

    public static int getQuarter(Date date)
    {
        int quarter = 0;
        int month = getMonth(date);
        if(month >= 1 && month <= 3)
            quarter = 1;
        else
        if(month >= 4 && month <= 6)
            quarter = 2;
        else
        if(month >= 7 && month <= 9)
            quarter = 3;
        else
        if(month >= 10 && month <= 12)
            quarter = 4;
        return quarter;
    }

    public static int daysOfMonth(int year, int month)
    {
        switch(month)
        {
        case 1: // '\001'
        case 3: // '\003'
        case 5: // '\005'
        case 7: // '\007'
        case 8: // '\b'
        case 10: // '\n'
        case 12: // '\f'
            return 31;

        case 4: // '\004'
        case 6: // '\006'
        case 9: // '\t'
        case 11: // '\013'
            return 30;

        case 2: // '\002'
            return !isLeap(year) ? 28 : 29;
        }
        return 0;
    }

    public static Date addDate(Date dDate1, Date dDate2)
    {
        long date1Ms = dDate1.getTime();
        long date2Ms = dDate2.getTime();
        long add = date1Ms + date2Ms;
        return new Date(add);
    }

    public static Date addDay(Date dDate, long iNbDay)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dDate);
        cal.add(5, (int)iNbDay);
        Date result = cal.getTime();
        return result;
    }

    public static Date addDuration(Date dDate, int iNbYear, int iNbMonth)
    {
        Date result = dDate;
        result = addMonth(result, iNbMonth);
        result = addYear(result, iNbYear);
        return result;
    }

    public static Date addDuration(Date dDate, int iNbYear, int iNbMonth, int iNbDay)
    {
        Date result = dDate;
        result = addDay(result, iNbDay);
        result = addMonth(result, iNbMonth);
        result = addYear(result, iNbYear);
        return result;
    }

    public static Date addDuration(Date dDate, int iNbYear, int iNbMonth, int iNbDay, int iNbHour)
    {
        Date result = dDate;
        result = addHour(result, iNbHour);
        result = addDay(result, iNbDay);
        result = addMonth(result, iNbMonth);
        result = addYear(result, iNbYear);
        return result;
    }

    public static Date addDuration(Date dDate, int iNbYear, int iNbMonth, int iNbDay, int iNbHour, int iNbMinute)
    {
        Date result = dDate;
        result = addMinute(result, iNbMinute);
        result = addHour(result, iNbHour);
        result = addDay(result, iNbDay);
        result = addMonth(result, iNbMonth);
        result = addYear(result, iNbYear);
        return result;
    }

    public static Date addDuration(Date dDate, int iNbYear, int iNbMonth, int iNbDay, int iNbHour, int iNbMinute, int iNbSecond)
    {
        Date result = dDate;
        result = addSecond(result, iNbSecond);
        result = addMinute(result, iNbMinute);
        result = addHour(result, iNbHour);
        result = addDay(result, iNbDay);
        result = addMonth(result, iNbMonth);
        result = addYear(result, iNbYear);
        return result;
    }

    public static Date addHour(Date dDate, long iNbHour)
    {
        long datems = dDate.getTime();
        long hourMs = iNbHour * 60L * 60L * 1000L;
        long newDateMs = datems + hourMs;
        Date result = new Date(newDateMs);
        return result;
    }

    public static Date addMinute(Date dDate, long iNbMinute)
    {
        long datems = dDate.getTime();
        long minuteMs = iNbMinute * 60L * 1000L;
        long newDateMs = datems + minuteMs;
        Date result = new Date(newDateMs);
        return result;
    }

    public static Date addMonth(Date dDate, int iNbMonth)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dDate);
        int month = cal.get(2);
        month += iNbMonth;
        int year = month / 12;
        month %= 12;
        cal.set(2, month);
        if(year != 0)
        {
            int oldYear = cal.get(1);
            cal.set(1, year + oldYear);
        }
        return cal.getTime();
    }

    public static Date addSecond(Date dDate, long iNbSecond)
    {
        long datems = dDate.getTime();
        long secondms = iNbSecond * 1000L;
        long newDateMs = datems + secondms;
        Date result = new Date(newDateMs);
        return result;
    }

    public static Date addYear(Date dDate, int iNbYear)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dDate);
        int oldYear = cal.get(1);
        cal.set(1, iNbYear + oldYear);
        return cal.getTime();
    }

    public static long dateDiff(String interval, Date dDate1, Date dDate2)
    {
        int desiredField = 0;
        int coef = 1;
        Date date1;
        Date date2;
        if(dDate1.getTime() > dDate2.getTime())
        {
            coef = -1;
            date1 = dDate2;
            date2 = dDate1;
        } else
        {
            date1 = dDate1;
            date2 = dDate2;
        }
        int field;
        if(interval.equals("yyyy"))
            field = 1;
        else
        if(interval.equals("m"))
            field = 2;
        else
        if(interval.equals("d"))
            field = 5;
        else
        if(interval.equals("y"))
            field = 5;
        else
        if(interval.equals("w"))
            field = 4;
        else
        if(interval.equals("ww"))
            field = 3;
        else
        if(interval.equals("h"))
        {
            field = 5;
            desiredField = 11;
        } else
        if(interval.equals("n"))
        {
            field = 5;
            desiredField = 12;
        } else
        if(interval.equals("s"))
        {
            field = 5;
            desiredField = 13;
        } else
        {
            return -1L;
        }
        Calendar calTmp = Calendar.getInstance();
        calTmp.setTime(date1);
        long nbOccurence = 0L;
        calTmp.add(field, 1);
        for(Date dateTemp = calTmp.getTime(); dateTemp.getTime() <= date2.getTime();)
        {
            calTmp.add(field, 1);
            dateTemp = calTmp.getTime();
            nbOccurence++;
        }

        if(desiredField == 11 || desiredField == 12 || desiredField == 13)
        {
            calTmp.setTime(date1);
            calTmp.add(field, (int)nbOccurence);
            Date dateTemp = calTmp.getTime();
            switch(desiredField)
            {
            case 11: // '\013'
                nbOccurence *= 24L;
                break;

            case 12: // '\f'
                nbOccurence = nbOccurence * 24L * 60L;
                break;

            case 13: // '\r'
                nbOccurence = nbOccurence * 24L * 60L * 60L;
                break;
            }
            calTmp.add(desiredField, 1);
            for(dateTemp = calTmp.getTime(); dateTemp.getTime() <= date2.getTime();)
            {
                calTmp.add(desiredField, 1);
                dateTemp = calTmp.getTime();
                nbOccurence++;
            }

        }
        return nbOccurence * (long)coef;
    }

    public static long dateDiff(Date date1, Date date2)
    {
        long date1ms = date1.getTime();
        long date2ms = date2.getTime();
        return date2ms - date1ms;
    }

    public static String dateToGMT(Date vDate, String sFormat)
        throws InvalidDateException
    {
        String resultString = "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(vDate);
        int iDay = cal.get(7);
        int iMonth = cal.get(2);
        String sWeekDay = "";
        String sMonth = "";
        String pattern = "";
        switch(iDay)
        {
        case 1: // '\001'
            sWeekDay = "Sunday";
            break;

        case 2: // '\002'
            sWeekDay = "Monday";
            break;

        case 3: // '\003'
            sWeekDay = "Tuesday";
            break;

        case 4: // '\004'
            sWeekDay = "Wednesday";
            break;

        case 5: // '\005'
            sWeekDay = "Thursday";
            break;

        case 6: // '\006'
            sWeekDay = "Friday";
            break;

        case 7: // '\007'
            sWeekDay = "Saturday";
            break;
        }
        switch(iMonth)
        {
        case 0: // '\0'
            sMonth = "Jan";
            break;

        case 1: // '\001'
            sMonth = "Feb";
            break;

        case 2: // '\002'
            sMonth = "Mar";
            break;

        case 3: // '\003'
            sMonth = "Apr";
            break;

        case 4: // '\004'
            sMonth = "May";
            break;

        case 5: // '\005'
            sMonth = "Jun";
            break;

        case 6: // '\006'
            sMonth = "Jul";
            break;

        case 7: // '\007'
            sMonth = "Aug";
            break;

        case 8: // '\b'
            sMonth = "Sep";
            break;

        case 9: // '\t'
            sMonth = "Oct";
            break;

        case 10: // '\n'
            sMonth = "Nov";
            break;

        case 11: // '\013'
            sMonth = "Dec";
            break;
        }
        if(sFormat.equals("RFC822") || sFormat.equals("RFC1123"))
        {
            pattern = "'" + sWeekDay.substring(0, 3) + "', dd '" + sMonth + "' yyyy HH:mm:ss 'GMT'";
            resultString = format(vDate, pattern);
        } else
        if(sFormat.equals("RFC850") || sFormat.equals("RFC1036"))
        {
            pattern = "'" + sWeekDay + "', dd-'" + sMonth + "'-yy HH:mm:ss 'GMT'";
            resultString = format(vDate, pattern);
        } else
        if(sFormat.equals("ASCTIME"))
        {
            pattern = "'" + sWeekDay.substring(0, 3) + " " + sMonth + "' d HH:mm:ss yyyy";
            resultString = format(vDate, pattern);
        } else
        {
            throw new InvalidDateException("dateToGmt: unknown RFC format");
        }
        return resultString;
    }

    public static String dateToSQL(Date vDate)
    {
        String sqlDate = "#" + format(vDate, "M/d/yyyy H:m:s") + "#";
        return sqlDate;
    }

    private static int formatMonth(String sMonth)
        throws InvalidDateException
    {
        if(sMonth.equalsIgnoreCase("jan"))
            return 0;
        if(sMonth.equalsIgnoreCase("feb"))
            return 1;
        if(sMonth.equalsIgnoreCase("mar"))
            return 2;
        if(sMonth.equalsIgnoreCase("apr"))
            return 3;
        if(sMonth.equalsIgnoreCase("may"))
            return 4;
        if(sMonth.equalsIgnoreCase("jun"))
            return 5;
        if(sMonth.equalsIgnoreCase("jul"))
            return 6;
        if(sMonth.equalsIgnoreCase("aug"))
            return 7;
        if(sMonth.equalsIgnoreCase("sep"))
            return 8;
        if(sMonth.equalsIgnoreCase("oct"))
            return 9;
        if(sMonth.equalsIgnoreCase("nov"))
            return 10;
        if(sMonth.equalsIgnoreCase("dec"))
            return 11;
        else
            throw new InvalidDateException("Invalid month name");
    }

    public static int getDay(Date d)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        return cal.get(5);
    }

    public static String getDayName(int iNDay)
        throws InvalidDateException
    {
        if(iNDay >= 1 && iNDay <= 7)
        {
            Calendar cal = Calendar.getInstance();
            cal.set(7, iNDay);
            return format(cal.getTime(), "EEEE");
        } else
        {
            throw new InvalidDateException("getDayName: day value must be between 1 and 7!");
        }
    }

    public static String getDayName(Date vDate)
    {
        return format(vDate, "EEEE");
    }

    public static int getElapsedTime(Date vDateBegin, Date vDateEnd, Date vDate)
        throws InvalidDateException
    {
        long begin = vDateBegin.getTime();
        long end = vDateEnd.getTime();
        long current = vDate.getTime();
        if(begin > end)
            throw new InvalidDateException("getElapsedTime : begin date must be inferior to end date !");
        if(current <= begin)
            return 0;
        if(current >= end)
        {
            return 100;
        } else
        {
            long result = (100L * dateDiff(vDateBegin, vDate)) / dateDiff(vDateBegin, vDateEnd);
            return (int)result;
        }
    }

    public static Date getGMTDate()
    {
        Date now = new Date();
        long nowMs = now.getTime();
        long gmtOffetsMs = getServerGMTOffset() * 60 * 1000;
        Date nowGMT = new Date(nowMs - gmtOffetsMs);
        return nowGMT;
    }

    public static int getGMTHour()
    {
        Date nowGMT = getGMTDate();
        int gmtHour = Integer.parseInt(format(nowGMT, "H"));
        return gmtHour;
    }

    public static long getGMTOffset(Date vDate)
    {
        Date nowGMT = getGMTDate();
        long nowGMTMs = nowGMT.getTime();
        long vDateMs = vDate.getTime();
        double diffMs = vDateMs - nowGMTMs;
        double diffSec = diffMs / 1000D;
        double diffMinute = Math.round(diffSec) / 60L;
        return Math.round(diffMinute);
    }

    public static int getMonth(Date d)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        return cal.get(2) + 1;
    }

    public static String getMonthName(int iMonth)
        throws InvalidDateException
    {
        if(--iMonth >= 0 && iMonth <= 11)
        {
            Calendar cal = Calendar.getInstance();
            cal.set(5, 1);
            cal.set(2, iMonth);
            return format(cal.getTime(), "MMMM");
        } else
        {
            throw new InvalidDateException("getMonthName : month value must be between 1 and 12!");
        }
    }

    public static String getMonthName(Date vDate)
    {
        return format(vDate, "MMMM");
    }

    public static Hashtable getRemainTime(Date vDate)
    {
        Date now = new Date();
        Hashtable numbers = new Hashtable();
        long iNbMilisecSeconds = vDate.getTime() - now.getTime();
        int m_iYears = 0;
        int m_iMonths = 0;
        int m_iDays = 0;
        int m_iHours = 0;
        int m_iMinutes = 0;
        int m_iSeconds = 0;
        int m_iMillisec = 0;
        int coef;
        if(iNbMilisecSeconds < 0L)
        {
            coef = -1;
            iNbMilisecSeconds = -iNbMilisecSeconds;
        } else
        {
            coef = 1;
        }
        Date m_date = new Date(now.getTime() + iNbMilisecSeconds);
        m_iYears = (int)dateDiff("yyyy", now, m_date);
        m_date = addYear(m_date, -m_iYears);
        if((dateDiff("m", now, m_date) < 0L || dateDiff("d", now, m_date) < 0L || dateDiff("h", now, m_date) < 0L || dateDiff("n", now, m_date) < 0L || dateDiff("s", now, m_date) < 0L) && m_iYears != 0)
        {
            m_iYears--;
            m_date = addYear(m_date, 1);
        }
        m_iMonths = (int)dateDiff("m", now, m_date);
        m_date = addMonth(m_date, -m_iMonths);
        if((dateDiff("d", now, m_date) < 0L || dateDiff("h", now, m_date) < 0L || dateDiff("n", now, m_date) < 0L || dateDiff("s", now, m_date) < 0L) && m_iMonths != 0)
        {
            m_iMonths--;
            m_date = addMonth(m_date, 1);
        }
        m_iDays = (int)dateDiff("d", now, m_date);
        m_date = addDay(m_date, -m_iDays);
        if((dateDiff("h", now, m_date) < 0L || dateDiff("n", now, m_date) < 0L || dateDiff("s", now, m_date) < 0L) && m_iDays != 0)
        {
            m_iDays--;
            m_date = addDay(m_date, 1L);
        }
        m_iHours = (int)dateDiff("h", now, m_date);
        m_date = addHour(m_date, -m_iHours);
        if((dateDiff("n", now, m_date) < 0L || dateDiff("s", now, m_date) < 0L) && m_iHours != 0)
        {
            m_iHours--;
            m_date = addHour(m_date, 1L);
        }
        m_iMinutes = (int)dateDiff("n", now, m_date);
        m_date = addMinute(m_date, -m_iMinutes);
        if(dateDiff("s", now, m_date) < 0L && m_iMinutes != 0)
        {
            m_iMinutes--;
            m_date = addMinute(m_date, 1L);
        }
        m_iSeconds = (int)dateDiff("s", now, m_date);
        m_date = addSecond(m_date, -m_iSeconds);
        m_iMillisec = (int)(m_date.getTime() - now.getTime());
        if(m_iMillisec < 0 && m_iSeconds != 0)
        {
            m_iSeconds--;
            m_date = addSecond(m_date, 1L);
            m_iMillisec = (int)(m_date.getTime() - now.getTime());
        }
        numbers.put("year", new Integer(m_iYears * coef));
        numbers.put("month", new Integer(m_iMonths * coef));
        numbers.put("day", new Integer(m_iDays * coef));
        numbers.put("hour", new Integer(m_iHours * coef));
        numbers.put("minute", new Integer(m_iMinutes * coef));
        numbers.put("second", new Integer(m_iSeconds * coef));
        return numbers;
    }

    public static int getServerGMTOffset()
    {
        Calendar now = Calendar.getInstance();
        int era = now.get(0);
        int year = now.get(1);
        int month = now.get(2);
        int day = now.get(5);
        int dayofweek = now.get(7);
        int milliseconds = now.get(14);
        int resultMs = Calendar.getInstance().getTimeZone().getOffset(era, year, month, day, dayofweek, milliseconds);
        return resultMs / 1000 / 60;
    }

    public static String getServerTimeZone()
    {
        Calendar cal = Calendar.getInstance();
        TimeZone tz = cal.getTimeZone();
        return tz.getID();
    }

    public static int getWeek(Date vDate)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(vDate);
        int week = cal.get(3);
        return week;
    }

    public static int getWeekDay(Date d)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        return cal.get(7);
    }

    public static int getYear(Date d)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        int year = cal.get(1);
        int era = cal.get(0);
        if(era == 0)
            return -1 * year;
        else
            return year;
    }

    public static Date gmtToDate(String sDate)
        throws InvalidDateException
    {
        int totalNbSpace = 0;
        int iDay = 0;
        String month = "";
        int year = 0;
        int hours = 0;
        int minutes = 0;
        int seconds = 0;
        for(int i = 0; i < sDate.length(); i++)
            if(sDate.charAt(i) == ' ')
                totalNbSpace++;

        if(totalNbSpace == 5)
        {
            int beginSpace = 1 + sDate.indexOf(" ");
            int endSpace = sDate.indexOf(" ", beginSpace);
            iDay = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(" ", beginSpace);
            month = sDate.substring(beginSpace, endSpace);
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(" ", beginSpace);
            year = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(":", beginSpace);
            hours = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(":", beginSpace);
            minutes = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(" ", beginSpace);
            seconds = Integer.parseInt(sDate.substring(beginSpace, endSpace));
        } else
        if(totalNbSpace == 3)
        {
            int beginSpace = 1 + sDate.indexOf(" ");
            int endSpace = sDate.indexOf("-", beginSpace);
            iDay = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf("-", beginSpace);
            month = sDate.substring(beginSpace, endSpace);
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(" ", beginSpace);
            year = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            if(year < 70)
                year += 2000;
            else
                year += 1900;
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(":", beginSpace);
            hours = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(":", beginSpace);
            minutes = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(" ", beginSpace);
            seconds = Integer.parseInt(sDate.substring(beginSpace, endSpace));
        } else
        if(totalNbSpace == 4)
        {
            int beginSpace = 1 + sDate.indexOf(" ");
            int endSpace = sDate.indexOf(" ", beginSpace);
            month = sDate.substring(beginSpace, endSpace);
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(" ", beginSpace);
            iDay = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(":", beginSpace);
            hours = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(":", beginSpace);
            minutes = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.indexOf(" ", beginSpace);
            seconds = Integer.parseInt(sDate.substring(beginSpace, endSpace));
            beginSpace = 1 + endSpace;
            endSpace = sDate.length();
            year = Integer.parseInt(sDate.substring(beginSpace, endSpace));
        } else
        {
            throw new InvalidDateException("gmtToDate: invalid gmt date string!");
        }
        Calendar cal = Calendar.getInstance();
        cal.set(1, year);
        cal.set(2, formatMonth(month));
        cal.set(5, iDay);
        cal.set(11, hours);
        cal.set(12, minutes);
        cal.set(13, seconds);
        return cal.getTime();
    }

    public static boolean isLeap(int year)
    {
        boolean div4 = year % 4 == 0;
        boolean div100 = year % 100 == 0;
        boolean div400 = year % 400 == 0;
        return div4 && (!div100 || div400);
    }

    public static Date secondsTo(long lNbSeconds)
    {
        return new Date(lNbSeconds * 1000L);
    }

    public static Date subDate(Date dDate1, Date dDate2)
    {
        long date1Ms = dDate1.getTime();
        long date2Ms = dDate2.getTime();
        long diff = date1Ms - date2Ms;
        return new Date(diff);
    }

    public static Date truncateDate(Date dt)
    {
        if(dt == null)
        {
            return null;
        } else
        {
            Calendar cal = Calendar.getInstance();
            cal.setTime(dt);
            cal.set(cal.get(1), cal.get(2), cal.get(5), 0, 0, 0);
            return new Date((cal.getTimeInMillis() / 1000L) * 1000L);
        }
    }

    private static TimeZone gmt = new SimpleTimeZone(0, "GMT");
    private static final String EX_GMT_TO_DATE = "gmtToDate: invalid gmt date string!";
    private static final String EX_GET_MONTH_NAME = "getMonthName : month value must be between 1 and 12!";
    private static final String EX_GET_ELAPSED_TIME = "getElapsedTime : begin date must be inferior to end date !";
    private static final String EX_GET_DAY_NAME = "getDayName: day value must be between 1 and 7!";
    private static final String EX_FORMAT_MONTH = "Invalid month name";
    private static final String EX_DATE_TO_GMT = "dateToGmt: unknown RFC format";

}


/*
	DECOMPILATION REPORT

	Decompiled from: E:\work\SHDC\lib\common\bos\common.jar
	Total time: 47 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/